roblox_tutorialsfandomcom-20200215-history
ROBLOX Tutorials Wiki:Custom Water Simulation
Written by Spooksletsky/CounterforceStudio Contents * 1 Introduction * 2 Graphing * 3 Programming Introduction After I tweeted my first picture of my own Custom Water Simulation system over on @Spooksletsky, I received an amount of messages asking me the one question, "How is this done?". I was planning however to release this type of article over on my channel SpooksHD, but I never did. So, I decided to post it here, in this article I go over the basics on create your own simulation of water in ROBLOX Studio. Graphing By the title of this segment, you may be asking what does 'Graphing' even have to do with Roblox. The answer to this question is quite simple, it doesn't have anything to do with it. Though it may have without a doubt nothing to do with Roblox, it's one of our many tools to fabricate a visual display our equation/algorithm. The Math So, where do we begin? Well, we start out by covering the mathematical functions that will be used in our function. So, you may ask yourself, "Well, what mathematical function would produce results relevant to wave-like motions?" This is where we move into trigonometry, if you don't know what the trigonometric functions sine and cosine are, I'll go over it for you. Sine is a mathematical function to identify the adjacent of a right-angled triangle, but you may say, "But, we aren't using a triangle!", my answer to that is, we aren't. We are using sine though to find the value assigned to the sine of an angle. Cosine is somewhat the same, but instead you are finding the opposite of a triangle instead of the adjacent. These two functions in computer science use a series called the Taylor Series, both going and starting at two different integers. Though, I'm not covering the Taylor Series today nor' am I going over Sine and Cosine very much. Inputs & Variables So, with the two functions taken care of, I can go ahead and say that cosine and sine create wave-like motions. So, using an online graphing calculator we'll go ahead and find the sine of x, this return an ongoing wave. So, when you input 'sin(x)' you get that. You can see how it somewhat resembles a wave, but we can do better. So, we go ahead and decide the variables that will most realistically change our graph. So, I went ahead and added a few variables, Amplitude for overall height, Height for the needed height, Length for overall length, and Pressure for world pressure. What we'll do next is divide x by l or the length to be more exact, the reason for this is that we'll need a way to transform our x into a decimal, this is replacing for turning our x into a radian, the output will be greater or less than one based on our l or length. So, we now have 'sin(x/l)', to add in the height factor/amplitude we'll just go ahead and multiply the result of sine by a or amplitude, this basically changes how high/low the waves go. We'll have our equation looking like this now, 'sin(x/l)*a'. Doing this and having a set to 2.7 and l set to 3.4 your result looks like this: This result show our waves being longer and taller than the original, but we still have to include the pressure variables and height variables. Now to explain why we have the height variable, it's basically our way of raising points to a certain starting y value in the global space, pressure simply just adds in a small amplifier. So, surrounding 'sin(x/l)*a' in brackets will give us '(sin(x/l)*a)', we than multiply that by the cosine of p, or to put it simply, 'cos(p)', p of course stands for pressure in this case. After that we add in h which stands for height at the very end after 'cos(p)' and we are left with this, '(sin(x/l)*a)*cos(p)+h'. After that your result will end up like this: Programming We now have our complete equation to generate wave-like motions dependent on the x value inputted into our machine, but how will we be able to use it in the game? Well, this is answered quite simple. First, we develop a function called 'calc' and we input our variables into the enclosed brackets like so. ---- function calc(x,a,h,l,p) -- X Pos : Amplitude : Height : Length : Pressure return (math.sin(x/l)*a)*math.cos(p)+h end ----You may see this and ask yourself, "Well, we're only receiving the value which is returned to us after we input our x value. So, how do we turn this into something like a group of parts?" The answer to this is also very simple, we'll go ahead and make a function which will generate all the parts we will be using. Using a for loop and a last set offset variable it makes it possible. ---- function genPartOcean(d, l) --Detail : Length --Start is always Zero. local llo = d --Last Left Offset for n=2,l,1 do local w = Instance.new('Part') w.Name = 'Wave'..n w.Size = Vector3.new(2,2,512) w.CFrame = CFrame.new(llo, 0, 0) w.Parent = workspace.Wave --Or the model/folder you want to put these in. llo=llo+d --Detail is basically the offset increment. end startGen() --Our function that will be created later. end ----As you can see this for loop will create the parts we need, feel free to change size and default location. So, what we'll do now is create the function in which will play the animation/cframe sets that we need to make our ocean look alive. Doing this is quite simple, three for loops, x, one for tweening smoothly using interpolation, and one for going through all the parts in our model/folder. This mainly get's compiled down into this simple function. ---- function startGen() local t = 0.01695684343 --Default: 0.01695684343, simple reminder, for reverse changes for o=1,#workspace.Wave:GetChildren(),1 do --Go through all wave objects. local np = workspace.Wave'Wave'..o spawn(function() while wait() do for n=0,103,1 do --The x value I chose local part = np local sc = part.CFrame for i=0,1,0.1 do --Simple loop required for lerp/interpolation part.CFrame = sc:lerp(CFrame.new(part.Position.X,calc(n-(part.CFrame.X/4), 5.4,4,1.5,-1.4),part.Position.Z), i) wait(1*t) --Simple wait time end end end end) end end So, after that you basically have everything you need to be able to continue developing such a thing. It works quite well when you have the detail set to high, this would usually be opposite but since offset is determined by detail, the less you have the less the gpu/cpu has to work. Anyways, dependent on how many of these you have, your game might cause some lag, just use this during a cut scene or anything else, maybe a small level perhaps? Anyways, that is it I believe, what I covered was mainly all you needed to know about Custom Water Simulation. Category:Programming